import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/flattererUserdayModel.dart';

class RankingBloc implements BlocBase {

  // ##########  STREAMS  ##############

  /// 日榜
  StreamController _day = StreamController<List<FlattererUserdayModel>>.broadcast();
  Sink get _daySink => _day.sink;
  Stream get dayStream => _day.stream;

  /// 周榜
  StreamController _week = StreamController<List<FlattererUserdayModel>>.broadcast();
  Sink get _weekSink => _week.sink;
  Stream get weekStream => _week.stream;

  /// 总榜
  StreamController _total = StreamController<List<FlattererUserdayModel>>.broadcast();
  Sink get _totalSink => _total.sink;
  Stream get totalStream => _total.stream;

  // ##########  HANDLING  ##############

  RankingBloc(String token) {
    flattererUserday(token);
    flattererUserweek(token);
    flattererUser(token);
  }

  void dispose(){
    _day.close();
    _week.close();
    _total.close();
  }

  /// 日榜
  List<FlattererUserdayModel> dayRank = [];
  /// 周榜
  List<FlattererUserdayModel> weekRank = [];
  /// 总榜
  List<FlattererUserdayModel> totalRank = [];

  /// 获取日榜
  void flattererUserday(String token) async {
    List<FlattererUserdayModel> result = await CommonApis.flattererUserday(token);
    if (!_day.isClosed) {
      dayRank = result;
      _daySink.add(result);
    }
  }

  /// 获取周榜
  void flattererUserweek(String token) async {
    List<FlattererUserdayModel> result = await CommonApis.flattererUserweek(token);
    if (!_week.isClosed) {
      weekRank = result;
      _weekSink.add(result);
    }
  }

  /// 获取总榜
  void flattererUser(String token) async {
    List<FlattererUserdayModel> result = await CommonApis.flattererUser(token);
    if (!_total.isClosed) {
      totalRank = result;
      _totalSink.add(result);
    }
  }

}